package com.myebills.ebpsfrontend.cust.maxis;

import java.sql.*;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.myebills.ebpsfrontend.cust.controller.*;
import javax.servlet.http.HttpSession;
import com.ibm.logging.ILogger;
import com.ibm.logging.IRecordType;

public class CusPhoneBookListServlet_v1r00 extends EbppxTransactionServlet {

private final String addErrorMsg = "Contact with same phone number already existed in phonebook";
static int count = 0;
    public void doTransaction(HttpServletRequest req, HttpServletResponse res) throws ServletException {
        try {
        System.out.println("Calling Listing Servlet"+ count++ +" ********************************************");
        ILogger trcLogger = null;
        trcLogger = getTraceLogger(req);
        final String fName = "doTransaction";
        if (trcLogger.isLogging())
            trcLogger.entry(IRecordType.TYPE_INFO, this, fName);

        final HttpSession session = req.getSession(false);
        if (null == session)
                throw new IllegalStateException("no session established");

        //Get attribute from session for the logon user
        final EbppxEcsUser ecsUser = (EbppxEcsUser) session.getAttribute(EbppxConstants.ECS_USER);

        String act = req.getParameter("act")==null?"":req.getParameter("act");
        String currentPage = req.getParameter("currentPage");
        req.setAttribute("currentPage", currentPage);

                if (act.equals("Update")){
                   System.out.println("doing Update***");
                   doUpdate(req, res, ecsUser);
                } else if (act.equals("UpdateAll")){
                   System.out.println("doing Bulk Update***");
                   doUpdateAll(req, res, ecsUser);
                } else if (act.equals("Delete")){
                   System.out.println("doing Delete***");
                   doDelete(req, res, ecsUser);
                } else if (act.equals("Add")){
                   System.out.println("doing Add***");
                   doAdd(req, res, ecsUser);
               } 

        System.out.println("doing List***");
        doList(req, res, ecsUser);

        forwardToMappedUrl(req, res, EbppxConstants.URL_JSP_PARAM);
        }catch (Throwable t){
                throw new ServletException(t.toString(), t);
        }finally {
	} //finally

    }//doTransaction

    public void doDelete(HttpServletRequest req, HttpServletResponse res, EbppxEcsUser ecsUser) throws ServletException {
        try {
                String number = req.getParameter("number");
                PhoneBookDAO.getInstance().delete(ecsUser.getUserId(), number);
          }catch (Throwable t){
                throw new ServletException(t.toString(), t);
            }finally {
	    } //finally

    }
    public void doUpdate(HttpServletRequest req, HttpServletResponse res, EbppxEcsUser ecsUser) throws ServletException {
        try {
                String number = req.getParameter("number");
                String name = req.getParameter("name");
                String callType = req.getParameter("callType");
				System.out.println("Update name: " +name +" number: " +number + "callType: " + callType);
                PhoneBookDAO.getInstance().update(ecsUser.getUserId(), number, name,callType);

          }catch (Throwable t){
                throw new ServletException(t.toString(), t);
            }finally {
	    } //finally
    }

    public void doUpdateAll(HttpServletRequest req, HttpServletResponse res, EbppxEcsUser ecsUser) throws ServletException {
        try {
                String[] numbers = req.getParameterValues("number");
                String[] names = req.getParameterValues("name");
                String[] callTypes = req.getParameterValues("callType");
                PhoneBookDAO.getInstance().updateAll(ecsUser.getUserId(), numbers, names,callTypes);

          }catch (Throwable t){
                throw new ServletException(t.toString(), t);
            }finally {
	    } //finally
    }

    public void doList(HttpServletRequest req, HttpServletResponse res, EbppxEcsUser ecsUser) throws ServletException {
        try {
                Collection resultList = null;

                resultList = PhoneBookDAO.getInstance().list(ecsUser.getUserId());
                req.setAttribute("PhoneBookList", resultList);

            }catch (Throwable t){
                throw new ServletException(t.toString(), t);
            }finally {
	    } //finally
    }

    public void doAdd(HttpServletRequest req, HttpServletResponse res, EbppxEcsUser ecsUser) throws ServletException {
        try {
                String number = req.getParameter("number");
                String name = req.getParameter("name");
                String callType = req.getParameter("callType");
                System.out.println("Call type is "+callType);
                boolean inserted = PhoneBookDAO.getInstance().add(ecsUser.getUserId(), number, name,callType);
                if (inserted==false){
                   req.setAttribute("PhoneBookError", addErrorMsg);
                }

            }catch (Throwable t){
                throw new ServletException(t.toString(), t);
            }finally {
	    } //finally
    }


}
